import { login, logout, getInfo } from "@/api/user";
import { getToken, setToken, removeToken } from "@/utils/auth";
import { resetRouter } from "@/router";
import getters from "../getters";
import * as deviceApi from '@/api/link/device'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: "",
    avatar: "",
    userInfo: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : {},
    roles: false,
    noticeNum: localStorage.getItem('noticeNum') ? localStorage.getItem('noticeNum') : 0,
    nowNum: localStorage.getItem('nowNum') ? localStorage.getItem('nowNum') : 0,
  };
};

const state = getDefaultState();

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState());
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  USERINFO: (state, userInfo) => {
    state.userInfo = userInfo;
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  SET_NUMBER: (state, noticeNum) => {
    state.noticeNum++;
    localStorage.setItem('noticeNum', state.noticeNum)
  },
  REMOVE_NUMBER: (state, noticeNum) => {
    state.noticeNum = 0
    localStorage.removeItem('noticeNum')
  },
  NOW_COUNT(state, nowNum) {
    state.nowNum = nowNum
  }
};

const actions = {
  // user login
  login({ commit }, userInfo) {
    // const { username, password } = userInfo;
    return new Promise((resolve, reject) => {
      login(userInfo)
        .then(response => {
          const { data } = response;
          commit("SET_TOKEN", data.token);
          commit("USERINFO", data);
          localStorage.setItem('userInfo', JSON.stringify(data))
          setToken(data.token);
          resolve(data);
        })
        .catch(error => {
          reject(error);
        });
    });
  },
  getInfo({ commit }) {
    return new Promise(resolve => {
      commit("SET_ROLES", true);
      setTimeout(() => {
        resolve(true);
      }, 200);
    });
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.userInfo)
        .then(() => {
          removeToken(); // must remove  token  first
          resetRouter();
          localStorage.removeItem("permission_routes");
          commit("RESET_STATE");
          commit("SET_ROLES", false);
          localStorage.removeItem('userInfo')
          resolve();
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken(); // must remove  token  first
      commit("RESET_STATE");
      commit("SET_ROLES", false);
      resolve();
    });
  },
  addCountNum({ commit }, state) {
    if (state == 1) {
      commit("SET_NUMBER")
    } else {
      commit("REMOVE_NUMBER")

    }
  },
  getNowAlarm({ commit }, state) {
    return new Promise((resolve, reject) => {
      deviceApi.nowCount().then(res => {
        console.log(res,'res');
        commit('NOW_COUNT',res.data)
        localStorage.setItem('nowNum', res.data)
        resolve();
      }).catch(error => {
        reject(error);
      });
    })
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
